# Figures 1, S2, S3
# libs --------------------------------------------------------------------
library(here)
library(tidyverse)
library(haven)

# data --------------------------------------------------------------------
data <- read_dta(file = here('data/modified data/analysis.dta'))


# plot --------------------------------------------------------------------
# fig 1
data %>% 
  filter(state %in% c('CA', 'UT', 'WA')) %>% 
  group_by(state, year) %>%
  summarize(treat_share = mean(treat)) %>% 
  ggplot(aes(x = year, y = treat_share, color = state)) +
  geom_point() +
  geom_line() +
  theme_bw() +
  labs(x = 'Year', y = 'Share of Counties w All-Mail Elections') +
  annotate('text', x = 2004, y = 0.75, label = 'WA', color = 4) +
  annotate('text', x = 2012, y = 0.5, label = 'UT', color = 3) +
  annotate('text', x = 2016, y = 0.1, label = 'CA', color = 2) +
  theme(legend.position = 'none') +
  ggsave('repfig/figure1.png', width = 6, height = 4, units = 'in')

# fig S2
data %>% filter(prim == 0, state == 'CA', year != 2000) %>% 
  ggplot(aes(x = vbm_share)) +
  geom_histogram(binwidth = 0.05) + 
  facet_wrap(~year) +
  theme_bw() +
  scale_x_continuous(breaks = c(0, .5, 1), limits = c(0, 1.001)) +
  labs(x = 'Mail Votes/Turnout', y = 'Num of Counties')

# fig S3
data %>% filter(prim==0, state == 'WA', year <= 2010) %>% 
  mutate(vbm_share = ifelse(vbm_share > 1, 1, vbm_share)) %>% 
  ggplot(aes(x = vbm_share)) + 
  geom_histogram(binwidth = 0.05) + 
  labs(x = 'Mail Votes/Turnout', y = 'Num of Counties') + 
  theme_bw() + 
  facet_wrap(~year)
